class Map {
  constructor() {
    var that = this;
    that.markers = [];
    that.map = new qq.maps.Map(document.getElementById("container"), {
      center: new qq.maps.LatLng(39.916527, 116.397128),      // 地图的中心地理坐标。
      zoom: 1                                                 // 地图的中心地理坐标。
    });
  }

  getCityService(callback) {
    let that = this;
    let service = new qq.maps.CityService({
      complete: function (result) {
        if (callback) {
          callback(result);
        } else {
          that.map.setCenter(result.detail.latLng);
          let marker = new qq.maps.Marker({
            position: result.detail.latLng,
            animation: qq.maps.MarkerAnimation.DROP,
            map: that.map,
            content: result.detail.name
          });
          that.markers.push(marker);
        }
      },
      error:function (response) {
        alert(response);
      }
    });
    return service;
  }

  getSearchService(callback) {
    var that = this;
    let searchService = new qq.maps.SearchService({
      complete: function (results) {
        if (callback) {
          callback(results);
        } else {
          let pois = results.detail.pois;
          let latlngBounds = new qq.maps.LatLngBounds();
          for (let i = 0, l = pois.length; i < l; i++) {
            let poi = pois[i];
            latlngBounds.extend(poi.latLng);
            let marker = new qq.maps.Marker({
              map: that.map,
              position: poi.latLng
            });
            marker.setTitle(i + 1);
            that.markers.push(marker);
          }
          that.map.fitBounds(latlngBounds);
        }
      },
      error:function (response) {
        alert(response);
      }
    });
    return searchService;
  }

  getDrivingService(callback) {
    let service = new qq.maps.DrivingService({
      complete: function (response) {
        if (callback) {
          callback(response);
        } else {
          //todo
        }
      },
      error:function (response) {
        alert(response);
      }
    });
    return service;
  }

  getGeocoder(callback) {
    let geoCoder = new qq.maps.Geocoder({
      complete: function (response) {
        if (callback) {
          callback(response);
        } else {
          //todo
        }
      },
      error:function (response) {
        alert(response);
      }
    });
    return geoCoder;
  }

  clearOverlays(markers) {
    if (markers) {
      for (let i in markers) {
        markers[i].setMap(null);
      }
    } else {
      for (let i in this.markers) {
        this.markers[i].setMap(null);
      }
    }
  }
}

export function qqmap(lat, lng, zoom) {
  return new Map(lat, lng, zoom);
}

